<?php
include_once 'ZCollection.php';
class ZTrendCollection extends ZCollection
{
    /**
     * @var ZTrendCollection
     */
    private $itemsByMonth   = null;
    /**
     * @var ZTrendCollection
     */
    private $itemsByWeek    = null;
    /**
     * Total trend caching
     * @var int|null
     */
    private $totalTrend     = null;

    /**
     * Get Trend Item Index
     * @param $index
     * @return ZTrendItem
     */
    public function getIndex($index){
        return $this->items[$index];
    }

    /**
     * Add a message
     * @param $item ZTrendItem
     */
    public function addItem($item){
        $this->items[] = $item;
    }

    /**
     * Get array of items
     * @return ZTrendItem[]
     */
    public function getItems() {
        return $this->items;
    }

    /**
     * Get items that grouped by month
     * @return ZTrendCollection
     */
    public function getItemsByMonth() {
        if ($this->itemsByMonth === null) {
            $this->itemsByMonth = new ZTrendCollection();
            $lastMonth          = '';
            $lastDate           = null;
            $trend              = 0;

            foreach($this->items as $item){
                $month = $item->getDate()->format('Y M');

                if ($month == $lastMonth) {
                    $trend += $item->getTrend();
                }
                else {
                    if ($lastDate) {
                        $this->itemsByMonth->addItem(new ZTrendItem($lastDate, $trend));
                    }
                    $lastMonth  = $month;
                    $lastDate   = $item->getDate();
                    $trend      = $item->getTrend();

                    //Store the last date, but by the beginning day of month
                    //Calculate the different from the Monday of week
                    if ($dif = (int)$lastDate->format('j') - 1) {
                        $lastDate->sub(new DateInterval('P'.$dif.'D'));
                    }
                }
            }

            //Append the last group
            if ($lastDate) {
                $this->itemsByMonth->addItem(new ZTrendItem($lastDate, $trend));
            }
        }

        return $this->itemsByMonth;
    }

    /**
     * Get trend items that grouped by week
     * @return ZTrendCollection
     */
    public function getItemsByWeek(){
        if ($this->itemsByWeek === null) {
            $this->itemsByWeek  = new ZTrendCollection();
            $lastMonth          = '';
            $lastDate           = null;
            $trend              = 0;

            foreach($this->items as $item){
                $month = $item->getDate()->format('Y W');

                if ($month == $lastMonth) {
                    $trend += $item->getTrend();
                }
                else {
                    if ($lastDate) {
                        $this->itemsByWeek->addItem(new ZTrendItem($lastDate, $trend));
                    }
                    $lastMonth  = $month;
                    $trend      = $item->getTrend();
                    $lastDate   = $item->getDate();

                    //Store the last date, but by the beginning day of week
                    //Calculate the different from the Monday of week
                    if ($dif = (int)$lastDate->format('w')) {
                        $lastDate->sub(new DateInterval('P'.$dif.'D'));
                    }
                }
            }

            //Append the last group
            if ($lastDate) {
                $this->itemsByWeek->addItem(new ZTrendItem($lastDate, $trend));
            }
        }

        return $this->itemsByWeek;
    }

    /**
     * Get total trend
     * @return Int
     */
    public function getTotal(){
        if ($this->totalTrend === null) {
            $total  = 0;
            foreach ($this->items as $item){
                $total += $item->getTrend();
            }
            $this->totalTrend = $total;
        }

        return $this->totalTrend;
    }
}

?>